package com.heitian.ssm.dao.split;


import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Properties;

/**
 * @创建者：zzw
 * @创建时间 2019/3/7 0007
 * @描述 基于Mybatis的拦截器  拦截sql信息
 */
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
        @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
                RowBounds.class, ResultHandler.class }) })
public class DynamicDataSourceInterceptor implements Interceptor {
    // 数据库操作字符串的匹配，insert，update，delete
    private static final String REGEX = ".*insert\\u0020.*|.*delete\\u0020.*|.*update\\u0020.*";
    private Logger log = Logger.getLogger(DynamicDataSourceInterceptor.class);
    public Object intercept(Invocation invocation) throws Throwable {
        // 判断是否被事务管理
        boolean synchronization = TransactionSynchronizationManager.isActualTransactionActive();
        Object[] objects = invocation.getArgs();
        MappedStatement ms = (MappedStatement) objects[0];
        String lookupKey = DynamicDataSourceHolder.DB_MASTER;
        // 判断是否被事务管理
        if (!synchronization) {
            // 读操作
            if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
                // selectKey 为自增id查询主键(SELECT LAST_INSERT_ID())方法，使用主库
                if (ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
                    lookupKey = DynamicDataSourceHolder.DB_MASTER;
                } else {
                    //  如果执行到了这里说明就是没有被事务管理也没有指定主键Key，只能对sql
                    // 语句进行匹配规则
                    BoundSql boundSql = ms.getBoundSql(objects[1]);
                    String sql = boundSql.getSql().toLowerCase().replaceAll("\\t\\n\\r", " ");
                    if (sql.matches(REGEX)) {
                        lookupKey = DynamicDataSourceHolder.DB_MASTER;
                    } else {
                        lookupKey = DynamicDataSourceHolder.DB_SLAVE;
                    }
                }
            }
        } else {
            // 如果被事务管理说明 就是增删改，需要在 master 中操作
            lookupKey = DynamicDataSourceHolder.DB_MASTER;
        }
        log.info("设置方法详细信息:" + ms.getId() + "," + lookupKey + "," + ms.getSqlCommandType().name());
        //设置访问数据库类型 master 或者 slave
        DynamicDataSourceHolder.setDbType(lookupKey);
        return invocation.proceed();
    }

    public Object plugin(Object target) {
        // 如果执行的是增删改的操作就使用本拦截，如果不是就直接返回
        if (target instanceof Executor) {
            return Plugin.wrap(target, this);
        }
        return target;
    }

    public void setProperties(Properties properties) {

    }
}
